﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Drawing;
using Flame.Dlr;
using System.Text.RegularExpressions;
using Flame.Reflection;
using Flame.Controls;
using Flame.Controls.Common;

namespace Flame.Scintilla
{
    [Flame.Controls.Common.InputPlugin]
    public class ScintillaNETInput : ATextInput
    {
        class LanguageOptions
        {
            //public Color Color { get; set; }
            //public Color AutoColor { get; set; }
            public ScintillaNET.SmartIndent SmartIndent { get; set; }
        }

        const int IntraLineDistance = 0;
        const int LineGap = 5;


        ScintillaNET.Scintilla _scintilla = new ScintillaNET.Scintilla();
        ScintillaNETAutoComplete _autocomplete;
        ICallTip _callTip;
        public override Control Control { get { return _scintilla; } }
        public override IAutocomplete IAutocomplete { get { return _autocomplete; } }
        public override ICallTip ICallTip { get { return _callTip; } }

        public override int CurrentPos
        {
            get
            {
                return _scintilla.CurrentPos;
            }
            set
            {
                _scintilla.CurrentPos = value;
            }
        }

        public ScintillaNETInput(IExecEngines compilers)
            : base(compilers)
        {
            _callTip = new ScintillaNETCallTip(_scintilla.CallTip);
            _autocomplete = new ScintillaNETAutoComplete(compilers, _callTip, _scintilla);

           _scintilla.Folding.IsEnabled = false;
           _scintilla.Folding.UseCompactFolding = true;
           _scintilla.Margins.Margin1.IsMarkerMargin = false;
           _scintilla.Margins.Margin1.IsFoldMargin = false;

           _scintilla.Margins.Margin1.Width = 0;
          
            _scintilla.Caret.HighlightCurrentLine = true;
            _scintilla.ConfigurationManager.ClearMargins = true;
            _scintilla.Scrolling.ScrollBars = ScrollBars.None;
            _scintilla.LineWrapping.Mode = ScintillaNET.LineWrappingMode.None;
        }


        protected override void onLanguageChanged()
        {
            try
            {
                switch(Language.Name)
                {
                    case "NoneExec":
                        _scintilla.ConfigurationManager.Language = "None";
                        _scintilla.Indentation.SmartIndentType = ScintillaNET.SmartIndent.None;
                        break;
                    case "CSharpCompiler":
                    case "CSharpExec":
                    case "HttpServerExec":
                        _scintilla.ConfigurationManager.Language = "cs";
                        _scintilla.Indentation.SmartIndentType = ScintillaNET.SmartIndent.CPP;
                        break;
                    case "IronPythonExec":
                        _scintilla.ConfigurationManager.Language = "Python";
                        _scintilla.Indentation.SmartIndentType = ScintillaNET.SmartIndent.Simple;
                        break;
                    case "IronRubyExec":
                        _scintilla.ConfigurationManager.Language = "Ruby";
                        _scintilla.Indentation.SmartIndentType = ScintillaNET.SmartIndent.CPP;
                        break;
                    case "PowershellExec":
                        _scintilla.ConfigurationManager.Language = "Powershell";
                        _scintilla.Indentation.SmartIndentType = ScintillaNET.SmartIndent.CPP;
                        break;
                    default:
                        _scintilla.ConfigurationManager.Language =Language.ToString();
                        _scintilla.Indentation.SmartIndentType = ScintillaNET.SmartIndent.None;
                        break;
                }
            }
            catch { }
        }

        public override int CaretOnScreenPosY
        {
            get
            {
                return _scintilla.PointToScreen(new Point(0, _scintilla.PointYFromPosition(_scintilla.Caret.Position))).Y;
            }
        }

        public override int ContentHeight
        {
            get { return (_scintilla.Lines[0].Height + IntraLineDistance) * _scintilla.Lines.Count + LineGap; }
        }

        public override int caret_LineNumber { get { return _scintilla.Caret.LineNumber; } }
        public override int lines_Count { get { return _scintilla.Lines.Count; } }
        public override string selection_Text { get { return _scintilla.Selection.Text; } }
        public override string lines_Current_Text { get { return _scintilla.Lines.Current.Text; } }
    }
}
